// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var leftMenu = require("../leftMenu/leftMenu");
	var constant = require("constant");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./interestDelayDetail.css");
	var interestDelayId;
	var html_oppose_pickup = require("./html_opposeInterestDelay.html");
	var js_platform_main = require("./platform_main.js");
	var tools = require("tools");
	var init = function(page) {
		interestDelayId = page.params.interestDelayId;
		//		pickupId = 51;	
		inquireInterestDelayApplicationDetail();
		//		databind.fillInput($("#interestDelayDetail"), {action:"inquireInterestDelayApplicationDetail.json?interestDelayId="+interestDelayId});
//		leftMenu.init(6);
//		js_platform_main.init(0);
		$(".left").removeClass("none");
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//拒绝延期计息
		$("#interestDelayDetail .opposeBtn").on("click", function() {
			$("#popdialog").html(html_oppose_pickup);
			$("#oppose_interest_delay_dialog").attr("action", "opposeInterestDelayApplication");
			$("#oppose_interest_delay_dialog").find(".dialogTitle").html("拒绝延期计息");
			$("#oppose_interest_delay_dialog").find(".word").html("拒绝");
			
			dialog.showDialog($("#oppose_interest_delay_dialog"));
			databind.fillForm($("#oppose_interest_delay_dialog"),{interestDelayId:interestDelayId});
			
			tools.inquireCommonSelectorList(9, $(".applicantReson"));
			$("#oppose_interest_delay_dialog .cancel1").unbind("click").click(function() {
				dialog.hideDialog($("#oppose_interest_delay_dialog"));
			});
			$("#oppose_interest_delay_dialog .sure").unbind("click").click(function() {
				databind.submitDom($("#oppose_interest_delay_dialog"), function() {
					base.alert(1, "拒绝延期计息成功！");
					dialog.hideDialog($("#oppose_interest_delay_dialog"));
					router.route("main/interestDelayList.html");
				});
			});
		});

		//同意延期计息
		$("#interestDelayDetail .agreeBtn").on("click", function() {
			$("#popdialog").html(html_oppose_pickup);
			$(".interestDelayReason").remove();
			$("#oppose_interest_delay_dialog").attr("action", "consentInterestDelayApplication");
			$("#oppose_interest_delay_dialog").find(".dialogTitle").html("同意延期计息");
			$("#oppose_interest_delay_dialog").find(".word").html("同意");
			dialog.showDialog($("#oppose_interest_delay_dialog"));
			databind.fillForm($("#oppose_interest_delay_dialog"),{interestDelayId:interestDelayId});
			$("#oppose_interest_delay_dialog .cancel1").unbind("click").click(function() {
				dialog.hideDialog($("#oppose_interest_delay_dialog"));
			});
			$("#oppose_interest_delay_dialog .sure").unbind("click").click(function() {
				databind.submitDom($("#oppose_interest_delay_dialog"), function() {
					base.alert(1, "同意延期计息成功！");
					dialog.hideDialog($("#oppose_interest_delay_dialog"));
					router.route("main/interestDelayList.html");
				});
			});
		});
	};

	//延期计息申请明细
	var inquireInterestDelayApplicationDetail = function() {
		base.fnAjax({
			action: "inquireInterestDelayApplicationDetail.json",
			oData: {
				interestDelayId: interestDelayId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#interestDelayDetail"), data.datas[0]);
					databind.fillForm($("#oppose_interest_delay_dialog"), data.datas[0]);
					tools.inquireCommonSelectorList(9, $(".delayReson"));
					$(".delayReson").val(data.datas[0].delayReson);
					databind.disableInput($("#interestDelayDetail"));
					
					inquireBulkstockInventoryPageList(data.datas[0].financeId);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	//查询融资箱货列表
	var inquireBulkstockInventoryPageList = function(financeId){
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl1 = $("#interestDelayDetailBulkstockTpl").html();
					var html1 = juicer(tpl1, data);
					$("#interestDelayDetailBulkstockBody").html(html1);
					
					juicer.register("getValueByKey", constant.getValueByKey);
					 tpl1 = $("#interestDelayDetailInventoryTpl").html();
					 html1 = juicer(tpl1, data);
					$("#interestDelayDetailInventoryBody").html(html1);
					
					if($("#interestDelayDetailBulkstockBody").find("tr").length==1){
						$("#interestDelayDetailBulkstockBody").parent().addClass("none");
					};
					if($("#interestDelayDetailInventoryBody").find("tr").length==1){
						$("#interestDelayDetailInventoryBody").parent().addClass("none");
					};
					//详情绑定事件
					$(".detail").unbind("click").click(function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					//得到货箱总估值
					$.each($(".bulkstockValuations"), function(i, val) {
							var bulkstockValuations = 0;
							$.each($(this).parents("tr").find(".bulkstockValuation"), function(i, val) {
								bulkstockValuations = bulkstockValuations + parseFloat($(this).html());
							});
							$(this).html(bulkstockValuations);
						});
						
						//得到货箱总应付值
					$.each($(".financeValuations"), function(i, val) {
							var financeValuations = 0;
							$.each($(this).parents("tr").find(".financeValuation"), function(i, val) {
								financeValuations = financeValuations + parseFloat($(this).html());
							});
							$(this).html(financeValuations);
						});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	return {
		init: init
	};
});